package com.imooc.security.browser.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * @author JackYang
 * @description:
 * @create: 2020/6/15 7:51
 */
@Component
public class MyUserDetailsService implements UserDetailsService {
    
    @Autowired
    private PasswordEncoder bCryptPasswordEncoder;
    
    private Logger logger= LoggerFactory.getLogger(getClass());
    /** 
     * 主要用于根据用户名验证用户信息
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        logger.info(username);
        //模拟根据用户名从数据库查询用户信息，基本的如加密过的用户的密码、授权角色等
        
        //将查询到的用户信息组装成UserDetails对象
        String passwordEncode = bCryptPasswordEncoder.encode("123456");
        //User user = new User(username, passwordEncode, AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
        
        //更详细的创建User：用户名、密码、账号可用【没删除】？、账号没过期？、身份信息没过期？、账号没被锁定【冻结】？，授权(角色)。只要一个为false就不能登录
        User user = new User(username, passwordEncode, true, true, true, true,
                AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
        return user;
    }
}
